package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //查询回显基本信息
        NursingPlan nursingPlan = this.getById( id);
        //查询关联表的de数据得到projectPlan
        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId,id));

        NursingPlanVo vo = BeanUtil.toBean(nursingPlan,NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = projectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        vo.setProjectPlans(nursingProjectPlanVos);


        return vo;

    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        //用hutool拷贝属性
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        this.nursingPlanMapper.insert(nursingPlan);
        //插入关联项目关联plan中的id
        nursingPlanDto.getProjectPlans().forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        });

        boolean b = nursingProjectPlanService.saveBatch(nursingPlanDto.getProjectPlans());


        return b ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 修改计划基本信息
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);

        // 删除旧计划项目
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));

        // 添加新计划项目并绑定计划（增加非空判断）
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null) {  // 先判断是否为null，避免空指针
            projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setPlanId(nursingPlan.getId());
            });
            // 保存新计划项目
            boolean b = nursingProjectPlanService.saveBatch(projectPlans);
            return b ? 1 : 0;
        } else {
            // 如果没有新计划项目，也视为成功（或根据业务需求处理）
            return 1;
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteNursingPlanById(Integer id) {
        //删除护理计划基本信息
        this.removeById(id);
        //删除护理计划项目关联
        boolean b = nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        return b?1:0;
    }
}
